package com.seventh.associationsystem.controller;

import com.seventh.associationsystem.bean.*;
import com.seventh.associationsystem.service.AssociationService;
import com.seventh.associationsystem.service.UserService;
import com.seventh.associationsystem.util.PageUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("association")
public class AssociationController {
    @Autowired
    private AssociationService associationService;

    @Autowired
    private UserService userService;

    @PostMapping("improve")
    public HttpResult improve(@RequestParam String uid, @RequestParam(value = "description", required = false) String description, @RequestParam(value = "type", required = false) String type) {
        Association association = associationService.getByUid(uid);
        if (description == null) {
            return new HttpResult(ResultCode.NO_DESCRIPTION, "没有填写社团简介！");
        }
        if (association.getStatus() != AssociationStatus.CHECKED) {
            return new HttpResult(ResultCode.ILLEGAL_STATUS, "社团当前状态无效！");
        }
        try {
            association.setType(AssociationType.valueOf(type));
        } catch (IllegalArgumentException | NullPointerException e) {
            return new HttpResult(ResultCode.UNDEFINED_TYPE, "社团类型无效！");
        }
        association.setDescription(description);
        association.setStatus(AssociationStatus.OPEN);
        associationService.update(association);
        return new HttpResult(ResultCode.NO_ERROR);
    }

    @GetMapping("all")
    public Page<Association> getAllAssociations(@RequestParam(value = "page", defaultValue = "0") int page,
                                                @RequestParam(value = "pageSize", defaultValue = "5") int pageSize,
                                                @RequestParam(value = "pageSortField", required = false) String pageSortField,
                                                @RequestParam(value = "pageSortDirection", defaultValue = "ASC") String pageSortDirection) {
        return associationService.getAllPageable(new PageRequest(page, pageSize, PageUtils.getOrders(pageSortField, pageSortDirection)));
    }

    @GetMapping("type")
    public Page<Association> getAssociationsByType(@RequestParam(value = "type", defaultValue = "ALL") String type,
                                                   @RequestParam(value = "page", defaultValue = "0") int page,
                                                   @RequestParam(value = "pageSize", defaultValue = "5") int pageSize,
                                                   @RequestParam(value = "pageSortField", required = false) String pageSortField,
                                                   @RequestParam(value = "pageSortDirection", defaultValue = "ASC") String pageSortDirection) {
        if (type.equalsIgnoreCase("all"))
            return associationService.getAllPageable(new PageRequest(page, pageSize, PageUtils.getOrders(pageSortField, pageSortDirection)));
        return associationService.getByTypePageable(AssociationType.valueOf(type), new PageRequest(page, pageSize, PageUtils.getOrders(pageSortField, pageSortDirection)));
    }

    @GetMapping("mine")
    public Page<Association> getMyAssociations(@RequestParam(value = "page", defaultValue = "0") int page,
                                               @RequestParam(value = "pageSize", defaultValue = "5") int pageSize,
                                               @RequestParam(value = "pageSortField", required = false) String pageSortField,
                                               @RequestParam(value = "pageSortDirection", defaultValue = "ASC") String pageSortDirection) {
        UserDetails userDetails = (UserDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        return associationService.getByUser((User) userDetails, new PageRequest(page, pageSize, PageUtils.getOrders(pageSortField, pageSortDirection)));
    }

    @GetMapping("uid")
    public Association getAssociation(@RequestParam String uid) {
        return associationService.getByUid(uid);
    }

    @RequestMapping("findByName")
    public Association findByName(@RequestParam(value = "name") String name) {
        return associationService.findByName(name);
    }

    @RequestMapping("isUserExist")
    public String isUserExist(@RequestParam(value = "name") String name) {
        Association association = associationService.findByName(name);
        List<User> userList = association.getMembers();
        User user = ((User) SecurityContextHolder.getContext().getAuthentication().getPrincipal());
        System.out.println(userList.contains(user));
        if (userList.contains(user)) {
            return "Y";
        } else {
            return "N";
        }
    }

    @RequestMapping("getUserRole")
    public HttpResult getUserRole(@RequestParam String uid) {
        Association association = associationService.getByUid(uid);
        User user = userService.getCurrentUser();
        if (association == null)
            return new HttpResult(ResultCode.GENERAL_ERROR);
        if (!association.getMembers().contains(user))
            return new HttpResult(ResultCode.NO_ERROR, "OK", 0);
        if (association.getManagers().contains(user) || association.getChief().getUsername().equals(user.getUsername()))
            return new HttpResult(ResultCode.NO_ERROR, "OK", 2);
        return new HttpResult(ResultCode.NO_ERROR, "OK", 1);
    }
}
